#include "header.h"
#include <math.h>

#define C_BLACK				0x000000	//RGB(0,0,0)
#define C_WHITE				0xffffff		//RGB(255,255,255)
#define C_RED				0x0000ff	//RGB(255, 0, 0)
#define C_GREEN				0x00ff00	//RGB(0, 255, 0)
#define C_BLUE				0xff0000	//RGB(0, 0, 255)
#define C_RRED				0x8080ff	//RGB(255, 100, 100)
#define C_RGREEN			0x80ff80	//RGB(100, 255, 100)
#define C_RBLUE				0xff8000	//RGB(0, 121, 255)

#define Cn_CAPTION		1
#define Cn_BUTTON			2
#define Cn_TEXT				3
#define Cn_CLIST				4
	#define Cn_CLIST_LED	1
	#define Cn_CLIST_TEXT	2
#define Cn_IMAGEBOX		5

#define F_ITALIC				1
#define F_BOLD				2
#define F_CANCEL			4
#define F_UNDERLINE		8

class controls{
private:
	long con_cnt;
	typedef void(*CALLBACK_control)(int);
	Type_Control *head, *tail;
	Type_Control * findControl(int ID)
	{
		Type_Control *temp = head;
		while (temp->Next != tail)
		{
			if (temp->ID == ID)	break;
			temp = temp->Next;
		}
		return temp;
	}
public:
	int gList[20];
	int programInfo;

	void Initialization()
	{
		head = (Type_Control*)malloc(sizeof(Type_Control));
		tail = (Type_Control*)malloc(sizeof(Type_Control));
		head->Next = tail;
		tail->Next = tail;
		con_cnt = 0;
	}
	int CreateControl()
	{	Type_Control *temp = (Type_Control *)malloc(sizeof(Type_Control));
		temp->ID = con_cnt++;
		temp->Next = tail;
		findControl(-1)->Next = temp;
		return temp->ID;
	}
	void DeleteControl(int ID)
	{	free(findControl(ID));
	}
	void SetControlTag(int ID, int _int, long _long, UINT _UINT , char _string[])
	{	Type_Control *temp = findControl(ID);
		temp->_int = _int;
		temp->_long = _long;
		temp->_UINT = _UINT;
		strcpy(temp->_string, _string);
	}
	void SetControlTheme(int ID, long backColor, long foreColor, long effectColor)
	{	Type_Control *temp = findControl(ID);
		temp->backColor	= backColor;
		temp->foreColor		= foreColor;
		temp->effectColor	= effectColor;
	}
	void SetControlFont(int ID,int font_h,char font_name[],UINT style)
	{	Type_Control *temp = findControl(ID); 
		int value = style;
		if (value >= F_UNDERLINE)
		{
			temp->font.lfUnderline = 1;
			value -= F_UNDERLINE;
		}else	temp->font.lfUnderline = 0;
		if (value >= F_CANCEL)
		{
			temp->font.lfStrikeOut = 1;
			value -= F_CANCEL;
		}else	temp->font.lfStrikeOut = 0;
		if (value >= F_BOLD)
		{
			temp->font.lfWeight = 600;
			value -= F_BOLD;
		}else temp->font.lfWeight = 400;
		if (value >= F_ITALIC)
		{
			temp->font.lfItalic = 1;
			value -= F_ITALIC;
		}else	temp->font.lfItalic = 0;

		if (font_name != NULL)
			strcpy(temp->font.lfFaceName, font_name);

		temp->font.lfCharSet = HANGEUL_CHARSET;
		temp->font.lfQuality = ANTIALIASED_QUALITY;
		temp->font.lfClipPrecision = 0;
		temp->font.lfEscapement = 0;
		temp->font.lfHeight = font_h;
		temp->font.lfOrientation = 0;
		temp->font.lfOutPrecision = 0;
		temp->font.lfPitchAndFamily = 0;
		temp->font.lfWidth = 0;
	}
	void SetControl(int ID, int x, int y, int w, int h,char text[],UINT style,UINT format)
	{	Type_Control *temp = findControl(ID);
		temp->x		= x;
		temp->y		= y;
		temp->w	= w;
		temp->h	= h;
		strcpy(temp->text, text);
		temp->style = style;
		temp->format = format;
	}
	void Render(int ID, HDC hdc,int x,int y)
	{
		Type_Control *control;
		if (ID + 1)
		{
			control = findControl(ID);
			HDC mDC = CreateCompatibleDC(hdc);
			HBITMAP mBitmap = CreateCompatibleBitmap(hdc, control->w, control->h);
			HBITMAP mOldBitmap = (HBITMAP)SelectObject(mDC, mBitmap);
			PatBlt(mDC, 0, 0, control->w, control->h, C_WHITE);
			SetBkMode(mDC, TRANSPARENT);
			SetStretchBltMode(mDC, COLORONCOLOR);

			RECT rcDraw;
			HFONT hFont;
			HPEN hOldPen;
			HBRUSH hOldBrush;

			hOldPen	= (HPEN)SelectObject(mDC, CreatePen(0, 0, control->foreColor));
				Rectangle(mDC, 0, 0, control->w, control->h);
			DeleteObject(SelectObject(mDC, hOldPen));

			switch (control->style)
			{
				case Cn_CAPTION:
					{
					
					}
				case Cn_BUTTON:
					{

					}
				case Cn_TEXT:
					{

					}
				case Cn_CLIST:
					{
						int value = control->format;
						if (value >= Cn_CLIST_TEXT)
						{
							LOGFONT temp;
							temp = control->font;
							SetControlFont(ID, control->_int, 0, 0);
							hFont = (HFONT)SelectObject(mDC, CreateFontIndirect(&control->font));
								SetRect(&rcDraw, 0, 10, control->w-10, control->h);
								DrawText(mDC, control->_string, -1, &rcDraw, control->_UINT);
							DeleteObject(SelectObject(mDC, hFont));
							control->font = temp;
							value -= Cn_CLIST_TEXT;
						}
						if (value >= Cn_CLIST_LED)
						{
							hOldPen = (HPEN)SelectObject(mDC, CreatePen(0, 0, control->_long));
							hOldBrush = (HBRUSH)SelectObject(mDC, CreateSolidBrush(control->_long));
							Ellipse(mDC, control->w - 30, control->h / 2 - control->_int / 2, control->w - 30 + control->_int, control->h / 2 + control->_int / 2);
							DeleteObject(SelectObject(mDC, hOldBrush));
							DeleteObject(SelectObject(mDC, hOldPen));
							value -= Cn_CLIST_LED;
						}
						hFont = (HFONT) SelectObject(mDC,CreateFontIndirect(&control->font));
							SetRect(&rcDraw, 10, 5, control->w, control->h);
							DrawText(mDC, control->text, -1, &rcDraw, DT_LEFT);
						DeleteObject(SelectObject(mDC, hFont));
					}
				case Cn_IMAGEBOX:
					{

					}
			}

			BitBlt(hdc, x + control->x, y + control->y,  control->w,  control->h, mDC, 0, 0, SRCCOPY);

			SelectObject(mDC, mOldBitmap);
			DeleteObject(mBitmap);
			DeleteDC(mDC);
		}
		else
		{
			control = head->Next;
			while(control != tail){
				Render(control->ID, hdc,x,y);
				control = control->Next;
			} 
		}
	}
};
class UI_{
	private:
		HWND _hwnd;
		typedef struct Type_ui
		{	int x, y;
			int h, w;
		};
		typedef struct Type_Theme
		{	long foreColor;
			long backColor;
			long effectColor;
		};
	public:
		Type_ui topbar,subbar;
		Type_Theme theme;
		
		int _speed;
		int _nowState, _bState, xState;
		int _subNowState, _subBState, _subXState;
		int * frame[5];
		long xEf;

		void Initialization(HWND hwnd)
		{	topbar.x = 15;
			topbar.y = 1;
			subbar.x = 12;
			subbar.y = 0;
			

			theme.foreColor = C_RBLUE;
			theme.backColor = C_RBLUE;
			theme.effectColor = C_BLUE;

			_nowState = 2;
			_bState = 0;
			_subNowState = 0;
			_subBState = 0;

			_speed = 7;
			_hwnd = hwnd;
		}

		void move_topbar(int num)
		{
			if (abs(num * 76 + 10 - topbar.x) < 2)
			{
				_bState = _nowState;
				topbar.y = 0;
			}
			else
			{
				topbar.x += 2 * (num * 76 + 10 - topbar.x) / abs(num * 76 + 10 - topbar.x) + (num * 76 + 10 - topbar.x) / (22 - _speed*2);
				xState = (abs(num * 76 + 10 - topbar.x)) / (1 + abs(_bState - _nowState));
			}
		}
		void move_subbar(int num)
		{
			if (abs(num * 56 + 10 - subbar.x) < 2)
			{
				_subBState = _subNowState;
				subbar.y = 0;
			}
			else
			{
				subbar.x += 2 * (num * 56 + 10 - subbar.x) / abs(num * 56 + 10 - subbar.x) + (num * 56 + 10 - subbar.x) / (22 - _speed * 2);
				_subXState = (abs(num * 56 + 10 - subbar.x)) / (1 + abs(_subBState - _subNowState));
			}
		}
};